﻿// DigitalDesign
// Project: InformationBoard
// Developer: Montak Oleg
// Created: 09.11.2011
// Last change: 11.12.2011
// Description: implementation of PingService - inform clients about the need to update the information

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Server
{
    public class PingService : IPingService
    {
        private DataModelContainer dataModel;
        public PingService()
        {
            dataModel = Context.Instance;
        }

        public int Ping(int clientId)
        {
            try
            {
                int resultInfo = DataVersionManager.Instance.GetDataVersion(clientId);

                // set current time as LastPing time...
                var client = dataModel.ClientChanges.Single(c => c.Id == clientId);
                client.LastPing = DateTime.Now;
                dataModel.SaveChanges();

                return client.DataVersion;
            }
            catch (Exception ex)
            {
                Log.Add(this, ex);
                return 0; 
            }
        }

        public wClientChanx PingEx(int clientId)
        {
            try
            {
                var resultInfo = DataVersionManager.Instance.GetDataVersionEx(clientId);

                // set current time as LastPing time...
                var client = dataModel.ClientChanges.Single(c => c.Id == clientId);
                client.LastPing = DateTime.Now;
                dataModel.SaveChanges();

                return resultInfo;
            }
            catch (Exception ex)
            {
                Log.Add(this, ex);
                return new wClientChanx();
            }
        }
    }
}
